# 屏幕输入的参数，从上游PM获取
param (
    [string]$User="",
    [string]$User_Pwd="",
    [string]$DatabaseName="",
    [int]$First=100,
    [int]$Skip=0
)

$DATA_BACKUP_AGENT_HOME = (Get-ChildItem env:DATA_BACKUP_AGENT_HOME).Value
$Location = $DATA_BACKUP_AGENT_HOME + "\DataBackup\ProtectClient\Plugins\GeneralDBPlugin\bin\applications\exchange\ps1_scripts"
Set-Location -Path $Location
[string]$common_file_path = (Get-Location).Path + "\Common.ps1"
[string]$pssession_name = "GetMailboxDetailByDatabases"

# 引用Common.ps1
. $common_file_path
# 建立连接
if (!(NewConnection $User $User_Pwd $pssession_name)) {
    $Result["ReturnCode"] = "-1"
    $Result["Output"] = "Get Mailbox List Failed Because Create Connection Failed"
    return $Result | ConvertTo-Json -Compress -Depth 10
}

$mailboxList = @()
$mailbox_detail = Invoke-Command -Session $Session -ScriptBlock {Get-Mailbox -Database $using:DatabaseName | Select-Object -First $using:First -Skip $using:Skip ExchangeGuid, Name, DisplayName, Alias, PrimarySmtpAddress, RecipientType}
$mailboxList += $mailbox_detail
$Result["ReturnCode"] = "0"
$Result["Output"] = $mailboxList

ClosePSSession $pssession_name
return $Result | ConvertTo-Json -Compress -Depth 10